
package com.hlkj.pay.app.common.ext.aliyun;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.framework.extension.annotation.Extension;
import com.hlkj.pay.app.common.OssExtPt;
import com.hlkj.pay.app.common.ext.CommonExtConstant;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.dto.common.StorageConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;

/**
 * 阿里云存储
 */
@Slf4j
@Repository
@RequiredArgsConstructor
@Extension(bizId = CommonExtConstant.CHANNEL_ALIYUN)
public class AliyunOssExtPt implements OssExtPt {

    /**
     * 鉴权令牌
     *
     * @author fzr
     * @return String
     */
    public OSS ossClient(StorageConfig storageConfig) {
        // String endpoint = "oss-cn-hangzhou.aliyuncs.com";
        String endpoint = storageConfig.getEndpoint();
        String accessKeyId = storageConfig.getAccessKey();
        String accessKeySecret = storageConfig.getSecretKey();
        return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }

    /**
     * 上传文件
     *
     * @param multipartFile
     *            文件对象
     * @param key
     *            文件名称 20220331/11.png
     */
    public String upload(MultipartFile multipartFile, String key, StorageConfig storageConfig) throws IOException {
        try {
            PutObjectRequest putObjectRequest = new PutObjectRequest(storageConfig.getBucket(), key, new ByteArrayInputStream(multipartFile.getBytes()));
            PutObjectResult putObjectResult = this.ossClient(storageConfig).putObject(putObjectRequest);
            log.info("upload message:{}", JsonUtils.toJsonString(putObjectResult));
            return storageConfig.getDomain() + key;
        }
        catch (OSSException oe) {
            throw new IOException(oe.getMessage());
        }
        catch (Exception ce) {
            throw new IOException(ce.getMessage());
        }
        finally {
            if (this.ossClient(storageConfig) != null) {
                this.ossClient(storageConfig).shutdown();
            }
        }
    }

    @Override
    public String upload(MultipartFile multipartFile, CommonEnum.OssEnum ossEnum, String key, StorageConfig storageConfig) throws IOException {
        return upload(multipartFile, ossEnum.getFolder() + "/" + key, storageConfig);
    }

    @Override
    public String upload(byte[] fileArray, CommonEnum.OssEnum ossEnum, String key, StorageConfig storageConfig) throws IOException {
        try {
            key = ossEnum.getFolder() + "/" + key;
            PutObjectRequest putObjectRequest = new PutObjectRequest(storageConfig.getBucket(), key, new ByteArrayInputStream(fileArray));
            PutObjectResult putObjectResult = this.ossClient(storageConfig).putObject(putObjectRequest);
            log.info("upload message:{}", JsonUtils.toJsonString(putObjectResult));
            return storageConfig.getDomain() + key;
        }
        catch (OSSException oe) {
            throw new IOException(oe.getMessage());
        }
        catch (Exception ce) {
            throw new IOException(ce.getMessage());
        }
        finally {
            if (this.ossClient(storageConfig) != null) {
                this.ossClient(storageConfig).shutdown();
            }
        }
    }

    @Override
    public String uploadZip(List<String> fileList, CommonEnum.OssEnum ossEnum, String key, StorageConfig storageConfig) throws IOException {
        return "";
    }
}
